// src/router/index.js
import { createRouter, createWebHashHistory } from 'vue-router';
import EquipmentManagement from '../views/EquipmentManagement.vue'

const routes = [
    {
        path: '/',
        name: 'layout',
        component: () => import('../layout/IndexLayout.vue'),
        meta: { title: '首页' , requiresAuth: true },
        redirect: '/Analytics-Dashboard',
        children: [  
            {
                path: '/Analytics-Dashboard',
                name: 'DataAnalyticsDashboard',
                component: () => import('../views/DataAnalyticsDashboard.vue'),
                meta: { title: '数据统计', requiresAuth: true ,icon:"shujutongji"}
            },
            {
                path: '/ingredient-management',
                name: 'IngredientManagement',
                component: () => import('../views/IngredientManagement.vue'),
                meta: { title: '底料管理', requiresAuth: true, requiresAdmin: true,icon:"dianhuahaoma" }
            },
            {
                path: '/generate-number',
                name: 'GenerateNumber',
                component: () => import('../views/GenerateNumber.vue'),
                meta: { title: '生成号码', requiresAuth: true, requiresAdmin: true ,icon:"haomaguanli" }
            },
            {
                path: '/script-setup',
                name: 'ScriptSetup',
                component: () => import('../views/ScriptSetup.vue'),
                meta: { title: '脚本设置', requiresAuth: true, requiresAdmin: true ,icon:"jiaobenshezhi"}
            },
            {
                path: '/equipment-management',
                name: 'EquipmentManagement',
                component: EquipmentManagement,
                meta: { title: '设备管理', requiresAuth: true, requiresAdmin: true ,icon:"shebeiguanli"}
            },
            {
                path: '/account-file',
                name: 'AccountFile',
                component: () => import('../views/AccountFile.vue'),
                meta: { title: '账号文件', requiresAuth: true, requiresAdmin: true,icon:"training-myfiles" }
            },
            {
                path: '/vpn-file',
                name: 'vpnFile',
                component: () => import('../views/vpnFile.vue'),
                meta: { title: 'VPN管理', requiresAuth: true, requiresAdmin: true ,icon:"daili"}
            }
        ]
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('../views/Login.vue'),
        meta: { title: '登录' },
    }
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

// 全局导航守卫，用于设置页面标题和权限检查
router.beforeEach((to, from, next) => {
    document.title = to.meta.title || '默认标题'; // 设置页面标题

    const userData = JSON.parse(localStorage.getItem('userData'));
    const isLoggedIn = !!userData; // 检查用户是否登录
    const isAdmin = userData?.role === 'admin'; // 检查用户是否为 admin
    if (to.meta.requiresAuth && !isLoggedIn) {
        // 如果需要认证但用户未登录，重定向到登录页面
        next({ name: 'login' });
    } else if (to.meta.requiresAdmin && !isAdmin) {
        // 如果需要 admin 权限但用户不是 admin，重定向到数据统计页面或其他页面
        next({ name: 'DataAnalyticsDashboard' });
    } else {
        next(); // 允许访问
    }
});

export default router;
